#!usr/bin/python
# -*= coding: UTF-8 -*-

'''
属性访问
__getattr__(self, name)
-定义当用户试图获取一个不存在的属性时的行为

__getattribute__(self, name)
-定义当该类的属性被访问时的行为

__setattribute__(self, name)
-定义当一个属性被设置时的行为

__delattr__(self, name)
-定义当一个属性被删除时的行为
'''


class C:
    def __init__(self):
        self.x = 'X-man'

c = C()
print(c.x)

a = getattr(c, 'x', '木有这个属性')
b = getattr(c, 'y', '木有这个属性')
print(a)
print(b)

class C:
    def __init__(self, size = 10):
        self.size = size

    def getSize(self):
        return self.size

    def setSize(self, value):
        self.size = value

    def delSize(self):
        del self.size

    x = property(getSize, setSize, delSize)

c = C()
print(c)
c.x = 1
print(c.x)
#del c.x
print(c.size)


class C:
    def __getattribute__(self, name):  #定义当该类的属性被访问时的行为
        print("getattribute")
        return super().__getattribute__(name) #super的用法，自动查找父类，基类

    def __getattr__(self, name):
        print('getattr')

    def __setattr__(self, name, value):
        print('setattr')
        super().__setattr__(name, value) #super() 函数是用于调用父类(超类)的一个方法。
    def __delattr__(self, name):
        print('delattr')
        super().__delattr__(name)






